package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrCalcFeeUtil;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.GeneralSimCardTypeAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Sim卡类型属性联动处理类 field_name:200003678
 */
@Service
@AttrHandler(GeneralSimCardTypeAttrHandler.class)
public class GeneralSimCardTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IOperateSceneInstService operateSceneInstService;
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //企业自采
        KiteAttr enterPriseSelfPurchaseAttr = inst.getAttr("200003676");
        if (Objects.nonNull(enterPriseSelfPurchaseAttr)) {
            //企业自采选择值为【是】,卡商要展示
            if ("1_0000106".equals(enterPriseSelfPurchaseAttr.getValue())) {
                LocalAttrHelper.showAndNonnull(inst, "200004040");
            }else{
                if(!KiteStringUtils.equals("100007000",attr.getValue())){
                    LocalAttrHelper.hideAndNullable(inst, "200004040");
                }else{
                    LocalAttrHelper.showAndNonnull(inst, "200004040");
                }
            }
        }
        //卡号数据服务费
        KiteAttr cardFeeAttr = inst.getAttr("200003679");
        if (Objects.nonNull(cardFeeAttr)) {
            String simCardFee;
            if (KiteStringUtils.equalsAny(attr.getValue(), "100007000", "100007001")) {
                //eSim卡类型
                double eSimCardFeeDoubleValue = LocalAttrCalcFeeUtil.calcESimCardFee(inst);
                simCardFee = String.valueOf(eSimCardFeeDoubleValue);
            }
            else {
                //普通卡类型
                simCardFee = LocalAttrCalcFeeUtil.calcNormalSimCardFee(inst);
            }

            if (KiteStringUtils.isEmpty(simCardFee) || "0.0".equals(simCardFee)) {
                cardFeeAttr.setValue("0");
                cardFeeAttr.setValueDesc("0");
            }
            else {
                cardFeeAttr.setValue(simCardFee);
                cardFeeAttr.setValueDesc(simCardFee);
            }
        }
        KiteAttr simCardEntitySpec = inst.getAttr("200004041");
        KiteAttr presetElectronicCard = inst.getAttr("200004042");
        KiteAttr eachPhysicalCard = inst.getAttr("200004043");
        if (Objects.nonNull(simCardEntitySpec) && Objects.nonNull(presetElectronicCard)) {
            if (KiteStringUtils.equalsAny(attr.getValue(), "100007000")) {
                simCardEntitySpec.setShowType("D");
                simCardEntitySpec.setIsNull("F");
                presetElectronicCard.setShowType("D");
                presetElectronicCard.setIsNull("F");
            }else{
                simCardEntitySpec.setShowType("E");
                simCardEntitySpec.setIsNull("T");
                presetElectronicCard.setShowType("E");
                presetElectronicCard.setIsNull("T");
            }
        }
        if (Objects.nonNull(eachPhysicalCard)) {
            if (KiteStringUtils.equalsAny(attr.getValue(), "100007000", "100007001")) {
                eachPhysicalCard.setShowType("D");
                eachPhysicalCard.setIsNull("F");
                List<KiteAttrValue> kiteAttrValues = new ArrayList<>();
                List<AttrValue> attrValues  = AttrValueCache.get("2020000019");
                if(KiteStringUtils.equals("100007001",attr.getValue())){
                    attrValues = attrValues.stream().filter(attrValue -> !KiteStringUtils.equals("1_0000399",attrValue.getAttrValue())).collect(Collectors.toList());
                }
                attrValues.stream().forEach(attrValue ->{
                    KiteAttrValue kiteAttrValue = new KiteAttrValue();
                    kiteAttrValue.setAttrValue(attrValue.getAttrValue());
                    kiteAttrValue.setAttrValueName(attrValue.getAttrValueName());
                    kiteAttrValues.add(kiteAttrValue);
                });
                if(KiteListUtils.isNotEmpty(kiteAttrValues)){
                    eachPhysicalCard.setAttrValues(kiteAttrValues);
                    eachPhysicalCard.setValue(kiteAttrValues.get(0).getAttrValue());
                }
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), eachPhysicalCard);
            }else{
                eachPhysicalCard.setShowType("E");
                eachPhysicalCard.setIsNull("T");
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
